from selenium.webdriver import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait

from base.get_logger import GetLogger


log=GetLogger.get_logger()

class BasePage:
    """
    页面基类
    """
    def __init__(self,driver):
        self.__driver=driver
    #获取元素
    def get_element(self,locator)->WebElement:
        """
        :param locator: 定位器
        :return: 元素
        """
        log.info("正在定位元素：{}".format(locator))
        try:
            web_element=(WebDriverWait(self.__driver,5).until(lambda x:x.find_element(*locator)))
            return web_element
        except Exception as e:
            log.error(f"元素定位失败：{e}")
            raise e

    #输入框输入内容
    def input_value(self,locator,value):
        """
        :param locator: 定位器
        :param value: 输入内容
        :return:
        """
        log.info("正在输入内容：{}".format(value))

        ele=self.get_element(locator)
        ele.clear()
        ele.send_keys(value)
    #点击元素
    def click_element(self,locator):
        self.get_element(locator).click()
    #获取文本
    def get_element_text(self,locator):
        return self.get_element(locator).text
    #鼠标的点击
    def mouse_click(self,locator):
        ActionChains(self.__driver).click(self.get_element(locator)).perform()
    #判断元素是否被选中
    def element_is_selected(self,locator):
        return self.get_element(locator).is_selected()


